<?php

namespace eu\freeplace\page\model;

use eu\freeplace\page\model\ValueObject as ValueObject;

class DocumentVO extends ValueObject {

    const CLASS_NAME = "DocumentVO";
    const DB_TABLE = "document";

    private $ContentId     = null;
    private $Version       = null;
    private $GroupId       = null;
    private $FolderId      = null;
    private $DocumentType  = null;
    private $ViewType      = null;
    private $Editor        = null;
    private $appoval       = null;
    private $publish       = null;
    private $editable      = null;
    private $trashed       = null;
    private $CreationDate  = null;

    public function __construct($contentID = "") {
        global $logger;
        $logger->log("Calling instance of " . self::CLASS_NAME . "(" . $contentID . ")", "NOTICE");

        try {
            if (strlen($contentID) < 1)
                throw new Exception(self::CLASS_NAME . " - contentID was empty");

            $this->ContentId = $contentID;
        } catch (Exception $e) {

            $logger->log($e->getMessage(), "NOTICE");
        }
        return null;
    }

    public function __clone() {
        self::fetchDocument($this->ContentId);
    }

    public function __toString() {
        return "{/**TODO: implement me */}";
    }

    protected function createDatabaseTable() {
        $sql =
           "CREATE TABLE IF NOT EXISTS " . $this->getTablePrefix() . self::DB_TABLE . " (
            ContentId       BIGINT    NOT NULL,
            Version         BIGINT    NOT NULL DEFAULT 0,
            GroupId         BIGINT    NOT NULL,
            FolderId        BIGINT    NOT NULL DEFAULT 0,
            DocumentType    CHAR(255) NOT NULL DEFAULT 'Page',
            ViewType        CHAR(255) NOT NULL DEFAULT 'default',
            Editor          CHAR(255) NOT NULL DEFAULT 'unknown',
            appoval         BOOLEAN   ,
            publish         BOOLEAN   DEFAULT false,
            editable        BOOLEAN   DEFAULT true,
            trashed         BOOLEAN   DEFAULT false,
            CreationDate    timestamp NOT NULL DEFAULT NOW(),

            PRIMARY KEY(ContentId, Version)
        );";
        return $sql;
    }

    public function getClassAttributes() {
        $document['ContentId'] = $this->ContentId;
        $document['Version'] = $this->Version;
        $document['GroupId'] = $this->GroupId;
        $document['FolderId'] = $this->FolderId;
        $document['DocumentType'] = $this->DocumentType;
        $document['ViewType'] = $this->ViewType;
        $document['Editor'] = $this->Editor;
        $document['appoval'] = $this->appoval;
        $document['publish'] = $this->publish;
        $document['editable'] = $this->editable;
        $document['trashed'] = $this->trashed;
        $document['CreationDate'] = $this->CreationDate;
        return $document;
    }

    private function fetchDocument($contentID) {
        global $logger;
        $logger->log(self::CLASS_NAME . "().fetchDocument(" . $contentID . ")", "DEBUG");

        $table_name = $this->getTablePrefix() . self::DB_TABLE;
        $sql = "SELECT * FROM " . $table_name . " WHERE ContentId=" . $contentID;

        $logger->log("\t" . $sql, "DEBUG");

        $pdo = $this->getResult($sql);
        $result = $pdo->fetch();

        if($result != 0) {
            $this->Version       = $result['Version'];
            $this->GroupId       = $result['GroupId'];
            $this->FolderId      = $result['FolderId'];
            $this->DocumentType  = $result['DocumentType'];
            $this->ViewType      = $result['ViewType'];
            $this->Editor        = $result['Editor'];
            $this->appoval       = $result['appoval'];
            $this->publish       = $result['publish'];
            $this->editable      = $result['editable'];
            $this->trashed       = $result['trashed'];
            $this->CreationDate  = $result['CreationDate'];
        } else {
            $logger->log(self::CLASS_NAME." No Document for ID: " . $contentID . ")", "DEBUG");
        }
    }

    ### [TODO]##################################################################

    /**
     * TODO: Fetch all Version of a Document (content)
     */
    public function getAllVersionsOfADocument($contentID) {
        return $contentID;
    }

    /**
     * TODO: Create a new document.
     */
    public function createNewDocument() {}

    /**
     * TODO: Chabnge an existing document.
     */
    public function updateDocument() {}

    /**
     * TODO: Delete an existing document.
     */
    public function deleteDocument() {}

}//CLASS
?>
